<?php

class CMRPage
{
	
// constants
	protected $NeededForApproval	= 3;
	
	protected $Authenticator;
	protected $Database;
	
	protected $ResourceTypes;

/*	protected $URL;*/
	protected $StyleSheet = "../css/style.css";

	protected $SessionID;
	protected $User;
	protected $UserID;
	
	protected $Title = "Cathedral Music & Liturgy Resources";
	protected $Action = "";
	protected $subAction = "";
	
	protected $messageText = "";
	
	protected $isLoggedIn = false;
	protected $userGroup = 0;
	
	public function __construct($database)
	{
		$this->Database = $database;
		
		if(isset($_POST['Action']))
		{
			$this->Action = $_POST['Action'];
		}
		elseif(isset($_GET['Action']))
		{
			$this->Action = $_GET['Action'];
		}
		
		if(isset($_POST['SubAction']))
		{
			$this->subAction = $_POST['SubAction'];
		}
		elseif(isset($_GET['SubAction']))
		{
			$this->subAction = $_GET['SubAction'];
		}
//		echo "CMRPage Action=".$this->Action."<br>\n";
//		echo "CMRPage SubAction=".$this->subAction."<br>\n";

		$theAuthenticator = new CMRAuthenticator();
		$theAuthenticator->setDatabase($database);
		$theAuthenticator->setUserTable("Users");
		$this->setAuthenticator($theAuthenticator);
		if ($this->isAuthenticated())
		{
//			echo "authent";
			$this->setIsLoggedIn(true);
		}
		
		
	}

	
	public function setDatabase($aDatabase)
	{
		$this->Database = $aDatabase;
	}

	public function getDatabase()
	{
		return $this->Database;
	}
	

	public function setAuthenticator($anAuthenticator)
	{
		$this->Authenticator = $anAuthenticator;
	}
	
	public function getAuthenticator()
	{
		return $this->Authenticator;
	}

	
	public function isAuthenticated()
	{
		if(!strcmp($this->Action,"logoff"))
			return(false);
			
		if(!isset($this->Authenticator))
		{
//			echo "no authent";
			if(isset($_POST['User']))
				$this->User = $_POST['User'];
			if(isset($_POST['SessionID']))
				$this->SessionID = $_POST['SessionID'];
			return true;
		}
	
		try
		{
//			echo "hello";
			if(isset($_POST['SessionID'])&&($_POST['SessionID']!=''))
			{
//				echo "boo".$_POST['SessionID'].$_POST['User'].$_POST['UserID'];
				if($this->Authenticator->isLoggedOn($_POST['SessionID'],$_POST['User']))
				{
					$this->User = $_POST['User'];
					$this->SessionID = $_POST['SessionID'];
					$this->UserID = $this->Authenticator->getUserID();
					return true;
				}
				else
				{
	//				$this->Authenticator->displayLogOn();
					return false;
				}
			}
			if(isset($_POST['logonUsername']))
			{
//				echo "booy".$_POST['logonUsername'];
				if ($this->Authenticator->tryLogOn(
//					$_POST['group'],
					"",
					$_POST['logonUsername'],
					$_POST['logonPassword']
					))
				{
					$this->User = $this->Authenticator->getUser();
					$this->UserID = $this->Authenticator->getUserID();
					$this->SessionID = $this->Authenticator->getSessionID();
//					echo "sessionID = ".$this->Authenticator->getSessionID();
					return true;
				}
				else
				{
//					$this->Authenticator->displayLogOn();
					return false;
				}
			}
		}
		catch(Exception $error){echo $error;} // 
//		$this->Authenticator->displayLogOn();
		return false;
	}	

	public function setMessageText($messageText)
	{
		$this->messageText = $messageText;
	}
	
	
	public function setStyleSheet($aStyleSheet)
	{
		$this->StyleSheet = $aStyleSheet;
	}
	
	public function getAction()
	{
		return $this->Action;
	}

	public function getSubAction()
	{
		return $this->subAction;
	}

	public function getHTML()
	{
		if ($this->isLoggedIn)
		{
//			echo "authent";
			$sql = "SELECT m.UserID, m.UserGroupID, u.UserID, g.GroupName
				FROM 
				(UserGroupMembers AS m
				INNER JOIN Users AS u ON m.UserID = u.UserID)
				INNER JOIN UserGroups as g ON g.UserGroupID = m.UserGroupID
				WHERE u.UserID = ".$this->UserID;
		
			$this->Database->ExecuteSQL($sql);
			$row = $this->Database->getRow();
			$this->userGroup =$row['GroupName'];
		}
		
		
		/* get arrays */
		$enumResourceTypes = new Enumeration($this->Database,'ResourceTypes');
		$this->ResourceTypes = $enumResourceTypes->getArray();
		
		
//		echo $this->SessionID;
		$html = $this->getHTTPHeadersHTML();
		$html .= $this->getHeaderHTML();
		
		$html .= $this->getStartHTML();
		
		$html .= $this->getContentsDiv();
		$html .= $this->getFooterDiv();
		$html .= $this->getLogonDiv();
		$html .= $this->getSearchDiv();
		if($this->messageText!="")
		{
			$html .= $this->getMessageDiv();
		}
		if($this->userGroup!='Readers')
			$html .= $this->getAdminDiv();
//		$html .= $this->getHowToDiv();
		$html .= $this->getEndHTML();
		
		return $html;
	}
	
	protected function setIsLoggedIn($isLoggedIn)
	{
		$this->isLoggedIn = $isLoggedIn;
	}
	
	protected function getIsLoggedIn()
	{
		return $this->isLoggedIn;
	}

	protected function getUserGroup()
	{
		return $this->userGroup;
	}

	protected function getHTTPHeadersHTML()
	{
		// overridden if needed
		return "";
	}
	
	protected function getHeaderHTML()
	{
		$html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
		$html .= "<head>\n";
		$html .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
		$html .= "<title>".$this->Title."</title>\n";
		$html .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$this->StyleSheet."\" />\n";
		$html .= file_get_contents("Javascript/CMRPage.js");
		$html .= "</head>\n";
		return $html;
	}

	protected function getLogonDiv()
	{
		if(!$this->isLoggedIn)
		{
			$html = "<div id=\"logon\"><p>Logon: ";
			$input = new Textbox();
			$input->setName("logonUsername");
			$html .= $input->getHTML();
			$html .= " Password: ";
			$input = new Password();
			$input->setName("logonPassword");
			$html .= $input->getHTML();
			$input = new Button();
			$input->setName("logonButton");
			$input->setValue("Go");
			$input->setOnClick("Logon();");
			$html .= $input->getHTML();
			$html .= "</p></div>\n";
			return $html;
		}
		else
		{
			$html = "<div id=\"logon\"><p>";
			$html .= $this->User." (".$this->userGroup.")&nbsp;&nbsp;&nbsp;";
			$input = new Button();
			$input->setName("logoffButton");
			$input->setValue("Logoff");
			$input->setOnClick("Logoff();");
			$html .= $input->getHTML();
			$html .= "</p></div>\n";
			return $html;
		}
	}
	
	protected function getSearchDiv()
	{
		$html = "<div id=\"search\"><p>Quick search: ";
		$input = new Textbox(); 
		$input->setName("searchText");
		$html .= $input->getHTML();
		$input = new Button();
		$input->setName("searchButton");
		$input->setValue("Go");
		$input->setOnClick("Search();");
		$html .= $input->getHTML();
		$span = new Span();
		$span->setName("advancedSearch");
		$span->setText("Advanced Search");
		$span->setOnClick("AdvancedSearch();");
		$html .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$span->getHTML();
		$html .= "</p></div>\n";
		return $html;	
	}
	
	protected function getMessageDiv()
	{
		$html = "<div id=\"message\">";
		$span = new Span();
		$span->setName("messageClose");
		$span->setText("X");
		$span->setOnClick("MessageClose();");
		$html .= $span->getHTML();
		$html .= "<p>".$this->messageText."</p>".$this->messageText;
		$html .= "</div>\n";
		return $html;	
	}
	
	protected function getAdminDiv()
	{
		$html = "<div id=\"admin\">";
		$html .= "<h1>Admin</h1><hr>";
		
		$html .= "<p id=\"adminCommand1\" ";
		$html .= "onMouseOver=\"OnMouseOverAdmin('adminCommand1');\" ";
		$html .= "onMouseOut=\"OnMouseOutAdmin('adminCommand1');\" ";
		$html .= "onClick=\"OnClickAdmin('adminCreateResource');\" ";
		$html .= "class=\"menuAdmin\">Create Resource</p>";
		
		$html .= "<p id=\"adminCommand2\" ";
		$html .= "onMouseOver=\"OnMouseOverAdmin('adminCommand2');\" ";
		$html .= "onMouseOut=\"OnMouseOutAdmin('adminCommand2');\" ";
		$html .= "onClick=\"OnClickAdmin('adminEditResource');\" ";
		$html .= "class=\"menuAdmin\">Edit Resource</p>";
		
		$html .= "<p id=\"adminCommand3\" ";
		$html .= "onMouseOver=\"OnMouseOverAdmin('adminCommand3');\" ";
		$html .= "onMouseOut=\"OnMouseOutAdmin('adminCommand3');\" ";
		$html .= "onClick=\"OnClickAdmin('adminSubmitResource');\" ";
		$html .= "class=\"menuAdmin\">Submit Resource</p>";
		
/*		$html .= "<p id=\"adminCommand4\" ";
		$html .= "onMouseOver=\"OnMouseOverAdmin('adminCommand4');\" ";
		$html .= "onMouseOut=\"OnMouseOutAdmin('adminCommand4');\" ";
		$html .= "onClick=\"OnClickAdmin('adminViewHistoryResource');\" ";
		$html .= "class=\"menuAdmin\">View Resource History</p>";
*/	

		if(($this->userGroup=='Supervisors')||($this->userGroup=='Editors'))
		{
		$html .= "<hr>\n";
		$html .= "<p id=\"adminCommand6\" ";
		$html .= "onMouseOver=\"OnMouseOverAdmin('adminCommand6');\" ";
		$html .= "onMouseOut=\"OnMouseOutAdmin('adminCommand6');\" ";
		$html .= "onClick=\"OnClickAdmin('adminApproveResource');\" ";
		$html .= "class=\"menuAdmin\">Approve Resource</p>";
		}
		
		if($this->userGroup=='Supervisors')
		{
		$html .= "<hr>\n";
		$html .= "<p id=\"adminCommand4\" ";
		$html .= "onMouseOver=\"OnMouseOverAdmin('adminCommand4');\" ";
		$html .= "onMouseOut=\"OnMouseOutAdmin('adminCommand4');\" ";
		$html .= "onClick=\"OnClickAdmin('adminTest');\" ";
		$html .= "class=\"menuAdmin\">Test</p>";
		
		$html .= "<p id=\"adminCommand5\" ";
		$html .= "onMouseOver=\"OnMouseOverAdmin('adminCommand5');\" ";
		$html .= "onMouseOut=\"OnMouseOutAdmin('adminCommand5');\" ";
		$html .= "onClick=\"OnClickAdmin('adminSessionHistory');\" ";
		$html .= "class=\"menuAdmin\">Session History</p>";
		}
		
		$html .= "</div>\n";
		return $html;	
		
	}
	
	protected function getHowToDiv()
	{
		$html = "<div id=\"howto\">";
		$html .= "<p>How To - need to put something here</p>";
		$html .= "</div>\n";
		return $html;	
		
	}
	
	
	protected function getFooterDiv()
	{
		$xml = simplexml_load_file('version.xml');
		
		$html = "<div id=\"footer\">";
		$html .= "<p>CMR Version ".$xml->Version." (".$xml->Date.")</p>";
		$html .= "</div>\n";
		return $html;
	}

	
	 protected function getContentsDiv()
	{
/*		$html = "<td><h1>Home Page</h1>\n<p>This is the point of entry to the Cathedral Music Resources website. Select the option you wish.</p>";
		
		$sql = "SELECT m.UserID, m.UserGroupID, u.UserID, g.GroupName
				FROM 
				(UserGroupMembers AS m
				INNER JOIN Users AS u ON m.UserID = u.UserID)
				INNER JOIN UserGroups as g ON g.UserGroupID = m.UserGroupID
				WHERE u.UserID = ".$this->UserID;
		
		$this->Database->ExecuteSQL($sql);
		$row = $this->Database->getRow();
		
		$html .= "sessionID -> ".$this->SessionID."<br>User -> ".$this->User."<br>UserID -> ".$this->UserID."<br>Group -> ".$row['GroupName']."</td>";
		return $html;*/
		$html = "<div id=\"content\">\n";
		$html .= "<h1>Welcome</h1>\n";
		if(!$this->isLoggedIn)
		{
			$html .= "<p>This site aims to provide a one-stop shop for cathedral liturgists, musicians and administrative staff incorporating choral, organ, liturgical and professional resources. It is staffed by members of the Cathedral Organists' Association and Assistant Cathedral Organists, and supported by the <a href=\"http://www.fcm.org.uk\">Friends of Cathedral Music</a>.</p>";
			$html .= "<p>In this site you can find texts, translations, seasonal and other information about a range of choral music, and you can also make a seasonal search of organ music. There are scores of choral and organ music in the public domain. Liturgists will find material and ideas relating to cathedral service orders. There is material on outreach, teaching, publicity, the voice, programme notes, psalmody and publications. The site contains many links to other related websites.</p>";						
			
		}
		$html .= "<p>\n";
		
		$image = new Image();
		$image->setCSSClass("content");
		$image->setName("ImgChoral");
//		echo "logged in ??  ".$this->isLoggedIn;
		if($this->isLoggedIn)
		{
			$image->setSource("Images/ChoralA.png");
			$image->setOnClick("OnClickChoral();");
			$image->setOnMouseOver("OnMouseOverChoral();");
			$image->setOnMouseOut("OnMouseOutChoral()");
		}
		else {$image->setSource("Images/Choral.png");}		
		$html .= $image->getHTML()."\n";
		
		$image = new Image();
		$image->setCSSClass("content");
		$image->setName("ImgOrgan");
		if($this->isLoggedIn)
		{
			$image->setSource("Images/OrganA.png");
			$image->setOnClick("OnClickOrgan();");
			$image->setOnMouseOver("OnMouseOverOrgan();");
			$image->setOnMouseOut("OnMouseOutOrgan()");
		}
		else {$image->setSource("Images/Organ.png");}		
		$html .= $image->getHTML()."\n";
		
		$html .= "</p>\n";
		$html .= "<p class=\"floatstop\" />\n";
		$html .= "<p>\n";
		
		$image = new Image();
		$image->setCSSClass("content");
		$image->setName("ImgLiturgy");
		if($this->isLoggedIn)
		{
			$image->setSource("Images/LiturgyA.png");
			$image->setOnClick("OnClickLiturgy();");
			$image->setOnMouseOver("OnMouseOverLiturgy();");
			$image->setOnMouseOut("OnMouseOutLiturgy()");
		}
		else {$image->setSource("Images/Liturgy.png");}		
		$html .= $image->getHTML()."\n";
		
		$image = new Image();
		$image->setCSSClass("content");
		$image->setName("ImgProfessional");
		if($this->isLoggedIn)
		{
			$image->setSource("Images/ProfessionalA.png");
			$image->setOnClick("OnClickProfessional();");
			$image->setOnMouseOver("OnMouseOverProfessional();");
			$image->setOnMouseOut("OnMouseOutProfessional()");
		}
		else {$image->setSource("Images/Professional.png");}		
		$html .= $image->getHTML()."\n";
		
		$html .= "</p>\n";
		$html .= "<p class=\"floatstop\" />\n";
/*		$html .= "<p>\n";
		$html .= "Description of the website.";
		$html .= "</p>";*/
		$html .= "</div>\n";
		return $html;

	}
	protected function getStartHTML()
	{
		$html = "<body>\n";
		$POSTVariables = new PostVariables();
		$html .= $POSTVariables->getHTML();
//		$html .= "<form id=\"PageForm\" method=\"post\">\n";
		$html .= "<form enctype=\"multipart/form-data\" id=\"PageForm\" action=\"index.php\" method = \"POST\">\n";
		
		$SessionID = new Hidden();
		$SessionID->setName("SessionID");
		$SessionID->setValue($this->SessionID);
		$html .= $SessionID->getHTML();

		$User = new Hidden();
		$User->setName("User");
		$User->setValue($this->User);
		$html .= $User->getHTML();

		$UserID = new Hidden();
		$UserID->setName("UserID");
		$UserID->setValue($this->UserID);
		$html .= $UserID->getHTML();

		
		$Action = new Hidden();
		$Action->setName("Action");
//		$Action->setValue($this->Action);
		$html .= $Action->getHTML();

		return $html;
	}
	
	protected function getEndHTML()
	{
		$html = "<div id=\"HomeLogo\" onclick=\"Home();\"></div>\n";
		$html .= "<a href=\"http://www.fcm.org.uk\"><div id=\"FCMLogo\"></div></a>\n";
		$html .= "<a href=\"http://www.fcm.org.uk\"><div id=\"FCMText\"><p>This website has only been made possible through the generous financial support of the Friends of Cathedral Music</p></div></a>\n";
		$html .= "<div id=\"CMRLogo\"><p>Cathedral&nbsp;Music&nbsp;&amp;&nbsp;Liturgy&nbsp;Resources</p></div>\n";
		$html .= "</form>\n";
		$html .= "</body>\n";
		$html .= "</html>\n";
		return $html;
	}
}

?>